// pages/order/order.js
const {
  formatTime2,
  formatDate3,
  extractAndConvertChineseNumbers,
  extractNumberFromString,
} = require("../../utils/utils");

Page({
  /**
   * 页面的初始数据
   */
  data: {
    userInfo: null,
    shopPosition: "-1",
    orderFoodList: [],
  },

  // 获取添加的数据
  getData() {
    if (this.data.shopPosition == "-1") return;
    wx.showLoading({
      title: "请求中",
    });

    const queryConditions = {
      shopPosition: this.data.shopPosition,
      orderDate: formatDate3(new Date()),
      sendFood: true,
    };

    console.log("查询条件: ", queryConditions);
    getApp()
      .getDatabase()
      .collection("orderList")
      .where(queryConditions)

      .get()
      .then((res) => {
        wx.hideLoading();

        this.setData({
          orderFoodList: res.data,
        });

        console.log("再次请求成功" + JSON.stringify(res));
      });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {},

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {},

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    // 页面加载时读取本地存储的 shopPosition
    const savedShopPosition = wx.getStorageSync("shopPosition");
    if (savedShopPosition !== "") {
      this.setData({ shopPosition: savedShopPosition });
      setTimeout(() => {
        this.getData();
      }, 500);
    }
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {},

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {},

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    wx.showLoading({
      title: "加载中",
    });

    // 模拟异步请求
    setTimeout(() => {
      // 更新数据
      this.getData();

      // 停止下拉刷新
      wx.stopPullDownRefresh();
    }, 2000);
  },
});
